package cn.gok.hotelback.service.impl;

import cn.gok.hotelback.entity.RoomOrder;
import cn.gok.hotelback.mapper.RoomOrderMapper;
import cn.gok.hotelback.service.RoomOrderService;
import cn.gok.hotelback.mapper.RoomNumberMapper;
import cn.gok.hotelback.entity.RoomNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import cn.gok.hotelback.entity.OrderReview;
import cn.gok.hotelback.mapper.UserMapper;
import cn.gok.hotelback.entity.User;
import cn.gok.hotelback.mapper.OrderAppendReviewMapper;

@Service
public class RoomOrderServiceImpl implements RoomOrderService {
    @Autowired
    private RoomOrderMapper roomOrderMapper;
    @Autowired
    private RoomNumberMapper roomNumberMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderAppendReviewMapper orderAppendReviewMapper;

    @Override
    public boolean createOrder(RoomOrder order) {
        return roomOrderMapper.insertOrder(order) > 0;
    }

    @Override
    public List<RoomOrder> findOrdersByPhoneSuffix(String phoneSuffix) {
        return roomOrderMapper.selectByPhoneSuffix(phoneSuffix);
    }

    @Override
    public boolean checkin(String orderId, Integer roomId, String guestInfo) {
        RoomNumber room = roomNumberMapper.selectById(roomId);
        if (room == null || !"0".equals(room.getCurrentStatus())) return false;
        int updated = roomOrderMapper.updateCheckin(orderId, roomId, room.getRoomNumber(), guestInfo);
        int updatedRoom = roomNumberMapper.updateStatus(roomId, "1");
        return updated > 0 && updatedRoom > 0;
    }

    @Override
    public boolean checkout(String roomNumber) {
        // 1. 查找该房号的已入住订单（order_status = '2'）
        RoomOrder order = roomOrderMapper.selectCheckedInOrderByRoomNumber(roomNumber);
        if (order == null) return false;
        // 2. 更新订单状态为已退房（order_status = '3'）
        int updatedOrder = roomOrderMapper.updateCheckout(order.getOrderId());
        // 3. 更新房间状态为打扫中（current_status = '2'）
        RoomNumber room = roomNumberMapper.selectById(order.getRoomId());
        int updatedRoom = roomNumberMapper.updateStatus(room.getRoomId(), "2");
        return updatedOrder > 0 && updatedRoom > 0;
    }

    @Override
    public RoomOrder getCheckedInOrderByRoomNumber(String roomNumber) {
        return roomOrderMapper.selectCheckedInOrderByRoomNumber(roomNumber);
    }

    @Override
    public List<RoomOrder> findOrdersByUserId(Integer userId) {
        return roomOrderMapper.findOrdersByUserId(userId);
    }

    @Override
    public boolean submitReview(String orderId, Integer rating, String content) {
        return roomOrderMapper.insertReview(orderId, rating, content) > 0;
    }

    @Override
    public List<OrderReview> getUserReviews(Integer userId) {
        return roomOrderMapper.getUserReviews(userId);
    }

    @Override
    public boolean appendReview(String orderId, String content) {
        return roomOrderMapper.appendReview(orderId, content) > 0;
    }

    @Override
    public boolean updateReview(Integer reviewId, Integer rating, String content) {
        return roomOrderMapper.updateReview(reviewId, rating, content) > 0;
    }

    @Override
    public boolean deleteReview(Integer reviewId) {
        return roomOrderMapper.deleteReview(reviewId) > 0;
    }

    @Override
    public boolean deleteAllReviewsByReviewId(Integer reviewId) {
        String orderId = roomOrderMapper.getOrderIdByReviewId(reviewId);
        if (orderId == null) {
            return false;
        }
        // 先删除追评
        orderAppendReviewMapper.deleteByOrderId(orderId);
        // 再删除主体评价
        return roomOrderMapper.deleteReviewsByOrderId(orderId) > 0;
    }

    @Override
    public User getUserById(Integer userId) {
        return userMapper.findById(userId);
    }

    @Override
    public java.util.List<RoomOrder> findAllOrders() {
        return roomOrderMapper.selectAllOrders();
    }

    @Override
    public OrderReview getReviewByOrderId(String orderId) {
        return roomOrderMapper.selectReviewByOrderId(orderId);
    }
} 